
package com.wyzz.hr.base.wf.leaverequest;

import java.util.*;

import com.wyzz.hr.base.wf.AbstractWorkflow;
import com.wyzz.hr.bo.TemplateService;
import com.wyzz.hr.pojo.Emailsend;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.spring.SpringBeanFactory;

public class LrWorkflow extends AbstractWorkflow {

	public LrWorkflow() {
	}

	public LrWorkflow(String operate, String comment, Object obj) {
		this.operate = operate;
		this.comment = comment;
		WorkflowInit(operate, comment, (Leaverequest) obj);
	}

	public void setObjFields(AbstractWorkflow wf) {
		String operate = wf.getOperate();
		Object obj = wf.getFlowObj();
		Leaverequest lr = (Leaverequest) obj;
		lr.setLrStatus(wf.getStatus());
		lr.setLrNextApprover(wf.getNextApprover());
		lr.setLrSecurityNo(UUID.randomUUID().toString());
		if (operate.contains("create")) {
			lr.setLrNo(wf.getDocNo());
			lr.setLrId(UUID.randomUUID().toString());
			lr.setLrEmpDept(lr.getLrEmpNo().getEmpDeptNo());
			lr.setLrEmpLocationNo(lr.getLrEmpNo().getEmpLocationNo());
		}
	}

	public String getTemplateByNotify(AbstractWorkflow wf) {
		String operate = wf.getOperate();
		String etTitleNo = null;
		if (operate.contains("create") || operate.contains("update"))
			etTitleNo = "LeaveSubmitNotify";
		if (operate.contains("mgr-approve") || operate.contains("mgr-modify")
				|| operate.contains("gm-approve")
				|| operate.contains("gm-modify"))
			etTitleNo = "LeaveApproveNotify";
		else if (operate.equals("hr-approve"))
			etTitleNo = "LeaveBackup";
		else if (operate.equals("hr-confirm"))
			etTitleNo = "LeaveResumption";
		else if (operate.equals("hr-cancel"))
			etTitleNo = "LeaveCancel";
		else if (operate.contains("reject"))
			etTitleNo = "LeaveReject";
		return etTitleNo;
	}

	public String getTemplateByOperate(AbstractWorkflow wf) {
		String operate = wf.getOperate();
		String etTitleNo = null;
		if (operate.contains("create") || operate.contains("update"))
			etTitleNo = "LeaveSubmit";
		else if (operate.contains("mgr-approve")
				|| operate.contains("mgr-modify")
				|| operate.contains("gm-approve")
				|| operate.contains("gm-modify"))
			etTitleNo = "LeaveApprove";
		return etTitleNo;
	}

	public void initMailParams(Emailsend emailsend, AbstractWorkflow wf,
			String templateNo, Employee nextApprover) {
		Map params = new HashMap();
		String urlApprove = null;
		params.put("SENDER", wf.getOperator());
		params.put("APPLIER", wf.getApplier());
		Leaverequest lr = (Leaverequest) wf.getFlowObj();
		params.put("LEAVE", lr);
		params.put("URL", "examin/myExamins.action?tab=1");
		if (nextApprover != null)
			urlApprove = getConfirmUrl(nextApprover.getId(), lr.getLrId(), lr
					.getLrSecurityNo(), "leaverequest");
		params.put("MANAGER", nextApprover);
		params.put("APPROVEURL", urlApprove);
		TemplateService emailTemplateService = (TemplateService) SpringBeanFactory
				.getBean("ftlTemplateService");
		String content = emailTemplateService.getContent((new StringBuilder())
				.append(templateNo).append("_body").toString(), params);
		String title = emailTemplateService.getContent((new StringBuilder())
				.append(templateNo).append("_title").toString(), params);
		emailsend.setEsTitle(title);
		emailsend.setEsContent(content);
	}
}
